#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>


#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>


#define BUF_SIZE 256
void error_handling(char *msg);


int get_init_val(int op);
int get_res(int num1,int num2, int op);
bool is_operator(int ch);

int main(int argc, char *argv[])
{
    int serv_sock;
    int clnt_sock;
    int str_len;
    int i;

    struct sockaddr_in serv_adr;
    struct sockaddr_in clnt_adr;
    socklen_t clnt_adr_sz;

    int msg[BUF_SIZE];

    if ( argc != 2) {
        printf("Usage: %s <port> \n", argv[0]);
        exit(1);
    }

    serv_sock = socket(PF_INET, SOCK_STREAM, 0);
    if (serv_sock == -1) {
        error_handling("socket() error");
    }

    memset(&serv_adr,0, sizeof(serv_adr));
    serv_adr.sin_family=AF_INET;
    serv_adr.sin_addr.s_addr=htonl(INADDR_ANY);
    serv_adr.sin_port=htons(atoi(argv[1]));

    if (-1 == bind(serv_sock, (struct sockaddr * ) &serv_adr, 
        sizeof(serv_adr) )) {
        error_handling("bind() error");
    }

    if (listen(serv_sock, 5) == -1) {
        error_handling("listen() error");
    }

    clnt_adr_sz = sizeof(clnt_adr);
    
    clnt_sock=accept(serv_sock, (struct sockaddr *) &clnt_adr, &clnt_adr_sz);
    if (-1 == clnt_sock){
            error_handling("accept() error");
    }
    else {
            printf("Connected client successs \n");
    }
    
        /* read time cnt*/
        int read_cnt = 0;
        /* last res*/
        int num_res = 0;
        int tot_read_cnt = 0;

        int tot_num_cnt;
        int op;
        
        
        read(clnt_sock, &tot_num_cnt, sizeof(tot_num_cnt));
        printf("total numbers: %d\n", tot_num_cnt);
        for (int i = 0;i < tot_num_cnt;i++) {
            read(clnt_sock, &msg[i], sizeof(msg[i]));
            printf("get %dth num: %d\n", i + 1, msg[i]);
        }
        
        read(clnt_sock, &op, sizeof(op));
        
        if (!is_operator(op))  {
            error_handling("not operator!");
        }

        num_res = get_init_val(op);
        printf("get op: %c\n", op);
        for (int i = 0;i < tot_num_cnt; i++) {
            printf("get %dth number: %d\n", i + 1, msg[i]);
            num_res = get_res(num_res, msg[i], op);
        }
        printf("last res: %d\n", num_res);
        if ( -1 == write(clnt_sock,&num_res, sizeof(num_res))) {
            error_handling("write() num_res error!");
        }



        
    close(clnt_sock);
    close(serv_sock);

    return 0;
}
void error_handling(char *msg)
{
    fputs(msg, stderr);
    fputc('\n', stderr);
    exit(1);
}


int get_init_val(int op)
{
    if (op == '+' || op == '-')
        return 0;
    if (op == '*' || op == '/')
        return 1;
    
    return 0;
}
int get_res(int num1,int num2, int op)
{
    int res = 0;
    switch (op)
    {
    case '+':
        res = num1 + num2;
        break;
    case '-':
        res = num1 - num2;
        break;
    case '*':
        res = num1 * num2;
        break;
    case '/':
        res = num1 / num2;
        break;
    default:
        break;
    }
    return res;
}


bool is_operator(int ch) {

    const char *s_op = "+-*/";
    int len = strlen(s_op);
    int i = 0;
    
    for (int i = 0;i < len; ++i) {
        if ( s_op[i] == ch )
            return true;
    }

    return false;
}

